package app

import (
	"context"

	"gitee.com/mkwchecking/sr-core/config"
	"gitee.com/mkwchecking/sr-core/config/source"
)

type Option func(*AppInfo)

func defaultAppInfo() *AppInfo {
	return &AppInfo{
		cfg:  config.Get().Application,
		ctx:  context.Background(),
		meta: make(map[string]string),
	}
}

type AppInfo struct {
	cfg    *config.Application
	ctx    context.Context
	source source.ISource
	meta   map[string]string // merge from config and other
}

func WithContext(ctx context.Context) Option {
	return func(ai *AppInfo) {
		if ctx != nil {
			ai.ctx = ctx
		}
	}
}

func WithSource(src source.ISource) Option {
	return func(ai *AppInfo) {
		ai.source = src
	}
}

func WithConfig(cfg *config.Application) Option {
	return func(ai *AppInfo) {
		ai.cfg = cfg
	}
}

func WithMeta(meta map[string]string) Option {
	return func(ai *AppInfo) {
		origin := make(map[string]string)
		if ai.cfg != nil {
			for k, v := range ai.cfg.Meta {
				origin[k] = v
			}
		}
		for k, v := range meta {
			origin[k] = v
		}
		ai.meta = origin
	}
}
